// 对错误进行处理的函数
const errorTypes = require('../constants/error-types');

const errorHandler = (error, ctx) => {
  let status, message;

  switch (error.message) {
    case errorTypes.NAME_OR_PASSWORD_IS_REQUIRED:
      status = 400; // bad Request
      message = "用户名或者密码不能为空";
      break;
    case errorTypes.USER_ALREADY_EXISTS:
      status = 409; //conflict 冲突
      message = "用户名已经存在了";
      break;
    case errorTypes.USER_DOES_NOT_EXITS:
      status = 400; //bad Reauest
      message = "用户名不存在";
      break;
    case errorTypes.PASSWORD_IS_INCORRENT:
      status = 400; //bad Reauest
      message = "密码是错误的";
      break;
    case errorTypes.UNAUTHRIZATION:
      status = 401; //bad Reauest
      message = "无效token";
      break;
    case errorTypes.UNPERMISSION:
      status = 401; //bad Reauest
      message = "不具备操作权限";
      break;
    default:
      status = 404;
      message = "NOT FOUND";
  }
  ctx.status = status;
  ctx.body = message;
};


module.exports = errorHandler;